package com.sjzsqjy.asdtiang.action;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.sjzsqjy.asdtiang.db.ShareService;
import com.sjzsqjy.asdtiang.mysql.HosUser;

public class LoginAction extends ActionSupport {
	private HosUser user;
	private String result;
	private ShareService shareService;

	public String execute() throws Exception {
		user = shareService.login(user.getUsername(), user.getPasswd());
		if (null == user) {
			this.addFieldError("loginError", this.getText("login.error"));
			return INPUT;
		}
		ActionContext.getContext().getSession().put("user", user);
		if(user.getHosRoles().iterator().hasNext()){
			ActionContext.getContext().getSession().put("userRole", user.getHosRoles().iterator().next().getAuthority());
		}
		
		return SUCCESS;
	}
	public String logoutExecute() throws Exception {
		ActionContext.getContext().getSession().clear();
	
		return SUCCESS;
	}

	public void validate() {

		if (ActionContext.getContext().getSession().get("user") != null) {
			return;
		}
		if (null==this.user.getPasswd()||this.user.getPasswd().equals("")) {
			this.addFieldError("passEmpty", this.getText("password.empty"));
		}
		if (null==this.user.getUsername()||this.user.getUsername().equals("")) {
			this.addFieldError("userNameEmpty", this.getText("username.empty"));
		}

	}

	public HosUser getUser() {
		return user;
	}

	public void setUser(HosUser user) {
		this.user = user;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	public ShareService getShareService() {
		return shareService;
	}

	public void setShareService(ShareService shareService) {
		this.shareService = shareService;
	}

}
